package android.slc.sys.ui

import android.content.DialogInterface
import android.os.Bundle
import android.slc.appbase.api.slc.config.ApiConfig
import android.slc.appbase.ui.activity.base.AppMvvmBaseToolBarActivity
import android.slc.appbase.ui.utils.LoadingUtils
import android.slc.extras.system.config.ConstantsSystem
import android.slc.popup.SlcPopup2
import android.slc.popup.fast.SimpleLoadingBuilder
import android.slc.sys.R
import android.slc.sys.databinding.SysActivityApiConfigBinding
import androidx.appcompat.app.AlertDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.StringUtils

/**
 *
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/06/29 下午 4:31
 *
 */
@Route(path = ConstantsSystem.Path.PATH_API_CONFIG_ACTIVITY)
class ApiConfigActivity : AppMvvmBaseToolBarActivity<SysActivityApiConfigBinding, ApiConfigVm>() {

    companion object {
        private var preClickTime = 0L
        private var preClickCount = 0
        fun start() {
            val currentTime = System.currentTimeMillis()
            if (currentTime - preClickTime < 2000) {
                preClickCount++
                if (preClickCount == 5) {
                    preClickCount = 0
                    //启动
                    ActivityUtils.startActivity(ApiConfigActivity::class.java)
                }

                return
            }
            preClickCount = 1
            preClickTime = currentTime
        }
    }

    override fun setContentView(): Any {
        return R.layout.sys_activity_api_config
    }

    override fun onBindView(savedInstanceState: Bundle?) {
        super.onBindView(savedInstanceState)
        mSlcToolbar.setTitle(R.string.api_label_api_config_title)
        mSlcToolbar.inflateMenu(R.menu.sys_menu_config_api)
        mSlcToolBarDelegate.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.action_save -> {
                    showSaveRebootDialog()
                }
            }
            return@setOnMenuItemClickListener true
        }
    }

    override fun onRegisterLiveEvent() {
        super.onRegisterLiveEvent()
        viewModel.showSearchingDialog.observe(this) {
            if (it) {
                SlcPopup2.createLoadingDialogOperate()
                    .setOnDismissListener {
                        viewModel.cancelRecursionSearch()
                    }
                    .setKey(ApiConfig.Key.KEY_AUTO_SEARCH).show(
                        SimpleLoadingBuilder(ActivityUtils.getTopActivity())
                            .setMessage(R.string.api_label_searching_service)
                            .create()
                    )
            } else {
                LoadingUtils.dismissLoadingDialog(
                    ApiConfig.Key.KEY_AUTO_SEARCH
                )
            }
        }
        viewModel.showSearchEndDialog.observe(this) {
            SlcPopup2.createAlertDialogOperate()
                .show(
                    AlertDialog.Builder(this)
                        .setTitle(R.string.label_prompt)
                        .setMessage(it)
                        .setPositiveButton(R.string.action_i_know) { dialog, which ->
                            when (which) {
                                DialogInterface.BUTTON_POSITIVE -> {
                                    dialog.dismiss()
                                }
                            }
                        }
                        .create()
                )
        }
    }

    override fun onBindViewLater() {
        super.onBindViewLater()
        dataBinding.vm = viewModel
        dataBinding.hostActivity = this

    }

    /**
     * 选择服务api配置模式
     */
    fun selectConfigServiceMode() {
        val itemNames = StringUtils.getStringArray(R.array.api_config_mode_1)
        SlcPopup2.createAlertDialogOperate()
            .show(AlertDialog.Builder(this).setItems(itemNames) { dialog, which ->
                viewModel.setConfigServiceMode(which, itemNames[which])
                dialog.dismiss()
            }.create())
    }

    /**
     * 显示保存重启提示对话款
     */
    private fun showSaveRebootDialog() {
        SlcPopup2.createAlertDialogOperate().show(
            AlertDialog.Builder(this).setTitle(R.string.label_prompt)
                .setMessage(R.string.api_label_save_config_take_effect_hint)
                .setPositiveButton(R.string.action_continue) { dialog, which ->
                    dialog.dismiss()
                    viewModel.save()
                }
                .setNegativeButton(R.string.action_cancel) { dialog, which ->

                }
                .create())
    }

    override fun onBackPressed() {
        SlcPopup2.createAlertDialogOperate().show(
            AlertDialog.Builder(this).setTitle(R.string.label_prompt)
                .setMessage(R.string.api_label_save_config_hint)
                .setPositiveButton(R.string.action_save) { dialog, which ->
                    dialog.dismiss()
                    showSaveRebootDialog()
                }
                .setNegativeButton(R.string.action_cancel) { dialog, which ->

                }
                .setNeutralButton(R.string.action_exit_without_save) { dialog, which ->
                    dialog.dismiss()
                    finish()
                }
                .create())
        //super.onBackPressed()
    }

}